/* Copyright 2014 Jan Wolter */
#include "stack.h"
#include "tableau.h"
#include "found.h"
#include "stock.h"
#include "solve.h"
#include "game.h"

char *gamename= "Agnes Sorel";
char *gameopt= "B";
Stack *gathered= NULL;
Stack *temp= NULL;

int nplayed= 0;
int nwon= 0;
int cardsleft= 0;
int maxoff;
int mayfill= 0;
int baserank;

void setopt(char c)
{
    switch (c)
    {
	case 'B': mayfill++; break;
    }
}


/* This is the main program for setting up the current game */
void exec(int gather, int arif, int over, int cut)
{
    /* Could set things like ndeck, nsuit here */

    /* Build and shuffle the deck */
    Stack *shuf= mixcards(arif, over, cut);

    /* The first card will be the base card for our foundations */
    Card *basecard= popStack(shuf);
    baserank= basecard->n;

    /* Create the foundations */
    superfound= sf_constbase;
    for (int s= 0; s < nsuit; s++)
    {
	makeFound(baserank, s, +1, 13);
	/* If the suit of this foundation matches our basecard put it on */
	if (s == basecard->s)
	    pushStack(stacks+foundation[nfound-1].id, basecard);
    }

    /* Create the tableau */
    for (int i= 1; i <= 7; i++)
	makeTableau(i, FLIP_NONE, shuf,
	    S_SAMESUIT, -1, S_SAMESUIT, -1, baserank, mayfill ? F_ANYTHING : F_NONE,
	    BREAK_NOWHERE, 0);

    makeStock(DT_TABLEAU,1,1,shuf);

    freeStack(shuf);

    if (verbose > 1) printState(stdout, stacks);

    maxoff= 0;
    int win= solve(gather,0);

    if (verbose > 0) printf("maxoff=%d\n",maxoff);
    nplayed++;
    cardsleft+= maxoff;
    if (win) nwon++;

    cleanFound();
    cleanTableau();
    cleanStock();
    cleanStacks();
    cleanVisited();
}

void gatherAll()
{
    if (gathered == NULL)
	gathered= allocStack(ncard);
    else
	gathered->n= 0;

    gatherFound(gathered);
    gatherTableau(gathered);
    gatherStock(gathered);
    if (gathered->n != ncard)
    {
	printf("Gather Failed - found %d cards\n",gathered->n);
	exit(1);
    }
}

/* How good is a move? Returns 0 if the move is completely safe and should
 * be made immediately without counting this position a a choice point for
 * backtracking. Positive values are lower for better moves. Negative values
 * mean the move should not be done at all.
 */
int rateMove(Stack *src, int index1, int len, Stack *dst)
{
    /* Deals are our least favorite move */
    if (src->type == STOCK) return 2;

    /* Always safe to move a card to the foundation in Agnes Sorel */
    if (dst->type == FOUND) return 0;

    /* Move into non-empty tableau spaces are also safe, but don't move on
     * basecard */
    if (dst->n > 0)
    {
	if (dst->c[dst->n - 1]->n == baserank)
	    return -1;
	else
	    return 0;
    }

    /* Otherwise we are moving into an empty tableau space. Ho hum */
    return 1;
}

void printState(FILE *f, Stack *stks)
{
    printFound(f, stks);
    printTableau(f, stks);
    printStock(f, stks);
}

int victory()
{
    int off= nOff();
    if (off > maxoff) maxoff= off;
    return (off == ncard);
}

/* Return a quality rating for the current solution. Bigger is better. Usually
 * it's just cards off. This is used to decide which solution to report in
 * cases where we never win.
 */
int quality()
{
    return nOff();
}

void summary()
{
    printf("Played:    %d games\n",nplayed);
    printf("Won:       %d games (%.2f%)\n",nwon,(float)nwon*100/nplayed);
    printf("Cards Off: %.2f\n",(float)cardsleft/nplayed);
}

/* Return a string that uniquely describes the current state. */
char *currstate()
{
    /* ncards for all the cards on things, 4 for tableaus, 1 for reserve,
     * 4 for list terminators.
     */
    char buf[ncard+20], *p= buf;

    /* Numbers of cards in each foundation pile */
    /* We add one to each count because we don't want any zeros */
    for (int k= 0; k < nfound; k++)
	*(p++)= stacks[foundation[k].id].n + 1;

    /* List of cards on each tableau pile - probably we should normalize
     * the order of tableau piles, sorting them by the first card on them,
     */
    for (int k= 0; k < ntableau; k++)
    {
	Stack *s= stacks + tableau[k].id;
	for (int i= 0; i < s->n; i++)
	    *(p++)= (s->c[i] - deck) + 1;
	*(p++)= ncard + 1; /* The card list terminator */
    }

    *p= '\0';

    return strdup(buf);
}
